home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-27 | 5.2 KB | 188 lines | [TEXT/CWIE] |
- /* SK8 © 1997 Apple Computer, Inc.
- This code is protected under the current SK8 License
- See http://sk8.research.apple.com/ for more information
- Apple Research Laboratories
- */
-
-
-
-
- public abstract class chunkedtext extends text {
-
- /**
- ** Constructors
- **/
- public chunkedtext(StringBuffer inString){
- super(inString);
- }
-
- public chunkedtext(String inString){
- super(inString);
- }
-
-
- /**
- ** collection protocol stuff...
- **/
-
- /**
- ** initialvisitstate
- **/
- public visitstate initialvisitstate() {
- return new chunkedtextvisitstate(this, 0);
- }
-
-
- /**
- ** isfinalvisitstate
- **/
- public boolean isfinalvisitstate(visitstate inState) {
- checkVisitStateType(inState);
- int nextChunkBounds[] = nextChunk(((textvisitstate)inState).mCurrentPosition);
- nextChunkBounds = nextChunk(nextChunkBounds[1] + 1);
- return (nextChunkBounds[0] == stringLength());
- }
-
-
- /**
- ** succeedingvisitstate
- **/
- public visitstate succeedingvisitstate(visitstate inState) {
- checkVisitStateType(inState);
-
- if (isfinalvisitstate(inState))
- return null;// throw new collectionexception("Can't get succeeding state from a final state");
- int nextChunkBounds[] = nextChunk(((textvisitstate)inState).mCurrentPosition);
- ((chunkedtextvisitstate)inState).mCurrentPosition = nextChunkBounds[1] + 1;
- ((chunkedtextvisitstate)inState).mCurrentChunkIndex++;
- return inState;
- }
-
-
- /**
- ** elementatvisitstate
- **/
- public Object elementatvisitstate(visitstate inState) {
- checkVisitStateType(inState);
- int ChunkedTextBoundries[] = nextChunk(((textvisitstate)inState).mCurrentPosition);
- char dest[] = new char[ChunkedTextBoundries[1] - ChunkedTextBoundries[0]];
- getChars(ChunkedTextBoundries[0],ChunkedTextBoundries[1],dest, 0);
- return new String(dest);
- }
-
-
- /**
- ** setelementatvisitstate
- **/
- public void setelementatvisitstate(visitstate inState, Object inElement) {
- checkVisitStateType(inState);
-
- char ch = '\u0000'; //so we don't get "maybe uninitialized" error
- boolean inputIsOK = false;
-
- if (inElement instanceof Character){
- ch = ((Character)inElement).charValue();
- inputIsOK = true;
- }
-
- if ((inElement instanceof String) && (((String)inElement).length()) == 1) {
- ch = ((String)inElement).charAt(0);
- inputIsOK = true;
- }
-
- if (! inputIsOK)
- throw new IllegalArgumentException("Incorrect value type");
-
- setNthChar(((textvisitstate)inState).mCurrentPosition, ch);
- }
-
- /**
- ** removevisitstate
- **/
- public void removevisitstate(visitstate inState) {
- checkVisitStateType(inState);
- int startingAt = ((chunkedtextvisitstate)inState).mCurrentPosition;
-
- int[] chunkBounds = nextChunk(startingAt);
- removeChars(startingAt , chunkBounds[1]);
-
- }
-
- /**
- ** insertatvisitstate
- **/
- public visitstate insertatvisitstate(visitstate inState, Object inElement, boolean inInsertAfter) {
- checkVisitStateType(inState);
-
- if (inElement instanceof Character)
- inElement = (((Character)inElement).toString());
-
- if (!(inElement instanceof String))
- throw new IllegalArgumentException("Can set elements of text only to Strings or Characters");
-
- String str = (String)inElement;
- if (! isChunkSeparator( str.charAt(str.length() -1)))
- str = str + defaultChunkSeparator();
-
- int insertionIndex = ((chunkedtextvisitstate)inState).mCurrentPosition;
- if (inInsertAfter) {
- int nextBounds[] = nextChunk(insertionIndex);
- insertionIndex = nextBounds[1] + 1;
- }
- insertString(insertionIndex, str);
- return inState;
- }
-
- /**
- ** indexatvisitstate
- **/
- public int indexatvisitstate(visitstate inState) {
- checkVisitStateType(inState);
- return (((chunkedtextvisitstate)inState).mCurrentChunkIndex);
- }
-
-
- /**
- ** visitstateatindex
- **/
- public visitstate visitstateatindex(int inIndex) {
- if (inIndex <= 0)
- throw new IndexOutOfBoundsException("ChunkedText index must be positive");
-
- int ChunkedTextBounds[] = nextChunk(0); //intialized for debugging purposes.
- int chi = 0;
- int chLast = stringLength();
- for (int wi = 1; wi <= inIndex; wi++){
- if (chi >= chLast)
- throw new IndexOutOfBoundsException("ChunkedText chunk index " + wi + " out of bounds");
- ChunkedTextBounds = nextChunk(chi);
- chi = ChunkedTextBounds[1] + 1;
- }
- return new chunkedtextvisitstate (this, ChunkedTextBounds[0], inIndex);
- }
-
-
- /**
- ** checkVisitStateType -protected-
- **/
- protected void checkVisitStateType(visitstate inState) {
- if (! (inState instanceof chunkedtextvisitstate))
- throw new IllegalArgumentException("Expecting a chunkedtextvisitstate, not a "
- + inState.getClass());
- }
-
- /**
- ** abstract handlers. must be overridden!
- **/
- protected abstract int[] nextChunk (int inLookingFrom) ;
-
- protected abstract boolean isChunkSeparator(char inCh);
- protected abstract char defaultChunkSeparator();
- /* defaultChunkSeparator and isChunkSeparator are needed so that an extra
- Separator may be added if necessary when inserting a chunk. For example,
- inserting "there" after word 1 of "hello world" should
- result in "hello there world" rather than "hello thereworld".
- However, if the final char of the chunk being inserted is already a chunk
- Separator , there is no need to insert a new separator.
- */
- }